home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Recursion in C pre-processor ?
- Date: 16 Jan 1996 20:19:09 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Jan16131909@qcd.lanl.gov>
- References: <30FC134D.3A8F@powertech.no>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: Geir Thomassen's message of Tue, 16 Jan 1996 13:06:21 -0800
-
- --text follows this line--
- In article <30FC134D.3A8F@powertech.no> Geir Thomassen
- <geirt@powertech.no> writes:
-
- <snip>
- #define DoFunc(n)\ /* Don't work !! */
- #if n\
- func();\
- DoFunc(n-1);\
- #endif
-
-
- Why ?? Does cpp stop expanding the #if n ... #endif in the first pass
- if n=0, or does it expand forever ??
-
- Two problems with this. First, compiler directives are recognized
- _before_ macro expansion, and not after. So, your #if and #endif will
- must not be recognized as such by the compiler. (Note also that
- backslash newlines are removed far before macro recognition etc.; and
- hence the #endif does not appear at the beginning of a line on
- expansion.) Of course, as no alternate meaning is possible in this
- context, the compiler is free to warn you and then choose to recognize
- the directive. Second, a macro name is protected against expansion
- when one instance of it is being expanded (or, rather when the
- expansion is being rescanned). Thus, the DoFunc(n-1) will not be
- expanded further. (And, in this case, there is an alternate meaning:
- you may link your code against another compilation unit defining int
- DoFunc(int)! So, a compiler must be very careful in interpreting this
- in a non-standard way.)
-
- As has been repeated often, the C preprocessor is very weak, and
- cannot be used to do such variant expansions.
-
- Does anyone have a better (working) idea ?? The idea of recursion is
- probably a wrong track! Please help !!
-
- Why do I want to do this ?? The function func() emits a NOP assembly
- instruction into the instruction stream. The code is for a slow 8051
- micro controller with 1 us instruction cycle, and the NOPs are used
- for timing. DoFunc(5) would give me 5 us delay, in just 5 bytes of code !!
-
- Surprising. Does your compiler optimize away the call to func?
- Otherwise, you would at least get a call and a return and not a single
- nop. If it does optimize away the call to func, why does it leave a
- nop behind?
-
- In any case, things like this are not possible in portable C. You can
- of course study your particular compiler and get it to do whatever it
- can do. The resulting program should not be called C, however :-)
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-